package com.ddwl.shop.dao;

import com.alibaba.fastjson.JSON;
import com.ddwl.common.dao.TkDao;
import com.ddwl.common.exception.GlobalException;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.schema.bo.shop.QueryResentShopBo;
import com.ddwl.schema.bo.shop.QueryShopBo;
import com.ddwl.schema.bo.shop.SearchShopBo;
import com.ddwl.shop.dao.mapper.ShopMapper;
import com.ddwl.shop.dao.model.Shop;
import com.ddwl.shop.dao.model.ShopApprove;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Collection;
import java.util.List;

/**
 * (Shop)表数据库访问层
 * @author wenbin
 */
@Slf4j
@Repository
public class ShopDao extends TkDao<ShopMapper, Shop> {

    public void updateShop(Shop shop) {
        Example example = new Example(ShopApprove.class);
        example.and().andEqualTo("id",shop.getId());
        Integer i =baseMapper.updateByExampleSelective(shop,example);
        if(i<1){
            log.error("修改店铺信息失败 shopId={}", JSON.toJSONString(shop.getId()));
            throw new GlobalException("修改店铺信息失败");
        }
    }

    public Shop queryShop(String id) {

        Example example = getExample();
        example.and()
                .andEqualTo("id",id);
        return baseMapper.selectOneByExample(example);
    }

    public Shop queryShop(Integer defaultTag) {
        Example example = getExample();
        example.and()
                .andEqualTo("defaultTag",defaultTag);
        return baseMapper.selectOneByExample(example);
    }

    public Shop queryShopDetailConsole(QueryShopBo bo) {
        return baseMapper.queryShopDetailConsole(bo);
    }

    public List<Shop> queryShopList(QueryShopBo bo) {
        return baseMapper.queryShopList(bo);
    }

    public List<Shop> getResentShop(QueryResentShopBo bo) {
        return baseMapper.getResentShop(bo);
    }


    public Shop getShopByUserId(String userId) {
        Shop shop = new Shop();
        shop.setUserId(userId);
        return selectOne(shop);
    }

    public List<Shop> searchShop(SearchShopBo bo) {
        Example example = getExample();
        example.and().andLike("name", MySqlUtil.allLike(bo.getName()));
        return baseMapper.selectByExample(example);
    }

    public List<Shop> getShopHasLogo() {
        Example example = getExample();
        example.and().andIsNotNull("logo");
        return baseMapper.selectByExample(example);
    }

    public List<Shop> getShopByUserIds(Collection<String> userIds) {
        Example example = getExample();
        example.and().andIn("userId", userIds);
        return baseMapper.selectByExample(example);
    }
}